﻿#define DEBUG
//undef DEBUG
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.Data;
using System.Data.SqlTypes;
using System.Collections.ObjectModel;
using System.Reflection;
using WFDataBaseProject.Helpers;
using System.Data.SqlClient;
using System.Diagnostics;
using System.Data.OleDb;
using System.Data.Odbc;
using System.IO;

namespace WFDataBaseProject.Controls
{
    /// <summary>
    /// Interaction logic for UCParametersCollectionEditor.xaml
    /// </summary>
    public partial class UCParametersCollectionEditor : UserControl
    {

        public UCParametersCollectionEditor()
        {
            InitializeComponent();
            lstParameters.ItemsSource = ParametersCollection;
        }
        private void UserControl_Loaded(object sender, RoutedEventArgs e)
        {
            if (ParametersCollection != null)
            {
                ParametersCollection.Clear();
            }
           
        }
        public QueryParametersCollection ParametersCollection
        {
            get { return (QueryParametersCollection)GetValue(ParametersCollectionProperty); }
            set { SetValue(ParametersCollectionProperty, value); }
        }

        // Using a DependencyProperty as the backing store for ParametersCollection.  This enables animation, styling, binding, etc...
        public static readonly DependencyProperty ParametersCollectionProperty =
            DependencyProperty.Register("ParametersCollection", 
            typeof(QueryParametersCollection), 
            typeof(UCParametersCollectionEditor),
            new PropertyMetadata(new QueryParametersCollection()));

        private void btnClear_Click(object sender, RoutedEventArgs e)
        {
            txtParamname.Clear();
            txtParamvalue.Clear();
        }

        private void btnAdd_Click(object sender, RoutedEventArgs e)
        {
            AddParameter();

        }

        
        private void btnCancel_Click(object sender, RoutedEventArgs e)
        {
            (this.Parent as Window).Close();
        }

        private void btnOK_Click(object sender, RoutedEventArgs e)
        {
            ParametersCollection.Clear();
            (this.Parent as Window).Close();
        }

        private void btnDelete_Click(object sender, RoutedEventArgs e)
        {
            DeleteParameter();
        }

        private void btnUpdate_Click(object sender, RoutedEventArgs e)
        {
            //DeleteParameter();
            //AddParameter();
            Lazy();
        }

        void Lazy()
        {
            StreamWriter writer = new StreamWriter(@"C:\temp\lazy.txt");
            int index = 0;
            SQLTypes.ConnectionType = TypeOfConnection.SqlServerConnection;
            foreach (var item in cmbParamtype.Items)
            {
                writer.WriteLine("case  " + index++  + ":\n");
                writer.WriteLine(string.Format("odbcparameter.DbType = DbType.{0};", item.ToString()));
                writer.WriteLine("break;");      
            }
            MessageBox.Show("Done");
        }

        private void DeleteParameter()
        {
            IDbDataParameter parameter = (IDbDataParameter)lstParameters.SelectedItem;
            ParametersCollection.Remove(parameter);
        }
        private void AddParameter()
        {
            switch (Communicator.ConnectionType)
            {

                #region oledb
                case TypeOfConnection.OledbConnection:
                    OleDbParameter oleparameter = new OleDbParameter(txtParamname.Text, txtParamvalue.Text);
                    switch (cmbParamtype.SelectedIndex)
                    {
                        case 0:

                            oleparameter.DbType = DbType.AnsiString;
                            break;
                        case 1:

                            oleparameter.DbType = DbType.Binary;
                            break;
                        case 2:

                            oleparameter.DbType = DbType.Byte;
                            break;
                        case 3:

                            oleparameter.DbType = DbType.Boolean;
                            break;
                        case 4:

                            oleparameter.DbType = DbType.Currency;
                            break;
                        case 5:

                            oleparameter.DbType = DbType.Date;
                            break;
                        case 6:

                            oleparameter.DbType = DbType.DateTime;
                            break;
                        case 7:

                            oleparameter.DbType = DbType.Decimal;
                            break;
                        case 8:

                            oleparameter.DbType = DbType.Double;
                            break;
                        case 9:

                            oleparameter.DbType = DbType.Guid;
                            break;
                        case 10:

                            oleparameter.DbType = DbType.Int16;
                            break;
                        case 11:

                            oleparameter.DbType = DbType.Int32;
                            break;
                        case 12:

                            oleparameter.DbType = DbType.Int64;
                            break;
                        case 13:

                            oleparameter.DbType = DbType.Object;
                            break;
                        case 14:

                            oleparameter.DbType = DbType.SByte;
                            break;
                        case 15:

                            oleparameter.DbType = DbType.Single;
                            break;
                        case 16:

                            oleparameter.DbType = DbType.String;
                            break;
                        case 17:

                            oleparameter.DbType = DbType.Time;
                            break;
                       default:
                        throw new NotImplementedException();
                    }
                    oleparameter.Size = SetSize(txtParamsize.Text);
                    switch (cmbParamdirection.SelectedIndex)
                    {
                        case 0:
                            oleparameter.Direction = ParameterDirection.Input;
                            break;
                        case 1:
                            oleparameter.Direction = ParameterDirection.Output;
                            break;
                        case 2:
                            oleparameter.Direction = ParameterDirection.InputOutput;
                            break;
                        case 3:
                            oleparameter.Direction = ParameterDirection.ReturnValue;
                            break;
                        default:
                            oleparameter.Direction = ParameterDirection.Input;
                            break;
                    }
                    ParametersCollection.Add(oleparameter);
#if DEBUG
                    Debug.Print("The oledb parameter is added");
#endif
                    break;
                #endregion

                #region sql
                case TypeOfConnection.SqlServerConnection:
                    SqlParameter sqlparameter = new SqlParameter(txtParamname.Text, txtParamvalue.Text);
                    sqlparameter.Size = SetSize(txtParamsize.Text);

                    switch (cmbParamtype.SelectedIndex)
                    {
                        case 0:

                            sqlparameter.DbType = DbType.AnsiString;
                            break;
                        case 1:

                            sqlparameter.DbType = DbType.Binary;
                            break;
                        case 2:

                            sqlparameter.DbType = DbType.Byte;
                            break;
                        case 3:

                            sqlparameter.DbType = DbType.Boolean;
                            break;
                        case 4:

                            sqlparameter.DbType = DbType.Currency;
                            break;
                        case 5:

                            sqlparameter.DbType = DbType.Date;
                            break;
                        case 6:

                            sqlparameter.DbType = DbType.DateTime;
                            break;
                        case 7:

                            sqlparameter.DbType = DbType.Decimal;
                            break;
                        case 8:

                            sqlparameter.DbType = DbType.Double;
                            break;
                        case 9:

                            sqlparameter.DbType = DbType.Guid;
                            break;
                        case 10:

                            sqlparameter.DbType = DbType.Int16;
                            break;
                        case 11:

                            sqlparameter.DbType = DbType.Int32;
                            break;
                        case 12:

                            sqlparameter.DbType = DbType.Int64;
                            break;
                        case 13:

                            sqlparameter.DbType = DbType.Object;
                            break;
                        case 14:

                            sqlparameter.DbType = DbType.SByte;
                            break;
                        case 15:

                            sqlparameter.DbType = DbType.Single;
                            break;
                        case 16:

                            sqlparameter.DbType = DbType.String;
                            break;
                        case 17:

                            sqlparameter.DbType = DbType.Time;
                            break;
                        default:
                            throw new NotImplementedException();
                    }
                    
                    switch (cmbParamdirection.SelectedIndex)
                    {
                        case 0:
                            sqlparameter.Direction = ParameterDirection.Input;
                            break;
                        case 1:
                            sqlparameter.Direction = ParameterDirection.Output;
                            break;
                        case 2:
                            sqlparameter.Direction = ParameterDirection.InputOutput;
                            break;
                        case 3:
                            sqlparameter.Direction = ParameterDirection.ReturnValue;
                            break;
                        default:
                            sqlparameter.Direction = ParameterDirection.Input;
                            break;
                    }
                    sqlparameter.Size = 30;
                    ParametersCollection.Add(sqlparameter);
#if DEBUG
                    Debug.Print("The sql parameter is added");
#endif
                    break;
                #endregion

                #region odbc
                case TypeOfConnection.ODBCConnection:
                    OdbcParameter odbcparameter = new OdbcParameter(txtParamname.Text, txtParamvalue.Text);
                    odbcparameter.Size = SetSize(txtParamsize.Text);
                    
                    switch (cmbParamtype.SelectedIndex)
	                {
                        case 0:

                            odbcparameter.DbType = DbType.AnsiString;
                            break;
                        case 1:

                            odbcparameter.DbType = DbType.Binary;
                            break;
                        case 2:

                            odbcparameter.DbType = DbType.Byte;
                            break;
                        case 3:

                            odbcparameter.DbType = DbType.Boolean;
                            break;
                        case 4:

                            odbcparameter.DbType = DbType.Currency;
                            break;
                        case 5:

                            odbcparameter.DbType = DbType.Date;
                            break;
                        case 6:

                            odbcparameter.DbType = DbType.DateTime;
                            break;
                        case 7:

                            odbcparameter.DbType = DbType.Decimal;
                            break;
                        case 8:

                            odbcparameter.DbType = DbType.Double;
                            break;
                        case 9:

                            odbcparameter.DbType = DbType.Guid;
                            break;
                        case 10:

                            odbcparameter.DbType = DbType.Int16;
                            break;
                        case 11:

                            odbcparameter.DbType = DbType.Int32;
                            break;
                        case 12:

                            odbcparameter.DbType = DbType.Int64;
                            break;
                        case 13:

                            odbcparameter.DbType = DbType.Object;
                            break;
                        case 14:

                            odbcparameter.DbType = DbType.SByte;
                            break;
                        case 15:

                            odbcparameter.DbType = DbType.Single;
                            break;
                        case 16:

                            odbcparameter.DbType = DbType.String;
                            break;
                        default:
                            throw new NotImplementedException();
	                }

                    switch (cmbParamdirection.SelectedIndex)
                    {
                        case 0:
                            odbcparameter.Direction = ParameterDirection.Input;
                            break;
                        case 1:
                            odbcparameter.Direction = ParameterDirection.Output;
                            break;
                        case 2:
                            odbcparameter.Direction = ParameterDirection.InputOutput;
                            break;
                        case 3:
                            odbcparameter.Direction = ParameterDirection.ReturnValue;
                            break;
                        default:
                            odbcparameter.Direction = ParameterDirection.Input;
                            break;
                    }
                    odbcparameter.Size = 30;
                    
                    ParametersCollection.Add(odbcparameter);
#if DEBUG
                    Debug.Print("The odbc parameter is added");
#endif
                    break;
                #endregion
                
                case TypeOfConnection.OtherConnection:
                    throw new NotImplementedException("Not implemented yet");
                default:
                    throw new NotImplementedException("Not implmented yet");
            }
        }

        private int SetSize(string input)
        {
            int output;
            bool result = int.TryParse(input, out output);
            return (result) ? output : 5;
        }

    }//Class
}//Namespace
